package controller

import (
	"encoding/json"
	"fmt"
	"github.com/hszz/education/service"
	"net/http"
)

var cuser User

func (app *Application) LoginView(w http.ResponseWriter, r *http.Request) {

	ShowView(w, r, "login.html", nil)
}

func (app *Application) Index(w http.ResponseWriter, r *http.Request) {

	ShowView(w, r, "index.html", nil)
}

func (app *Application) Help(w http.ResponseWriter, r *http.Request) {
	data := &struct {
		CurrentUser User
	}{CurrentUser: cuser}

	ShowView(w, r, "help.html", data)
}

// 用户登录
func (app *Application) Login(w http.ResponseWriter, r *http.Request) {
	loginName := r.FormValue("loginName")
	password := r.FormValue("password")

	var flag bool
	for _, user := range users {
		if user.LoginName == loginName && user.Password == password {
			cuser = user
			flag = true
			break
		}
	}

	data := &struct {
		CurrentUser User
		Flag        bool
	}{
		CurrentUser: cuser,
		Flag:        false,
	}

	if flag { // 登录成功
		ShowView(w, r, "index.html", data)
	} else { // 登录失败
		data.Flag = true
		data.CurrentUser.LoginName = loginName
		ShowView(w, r, "login.html", data)
	}
}

// 用户登出
func (app *Application) LoginOut(w http.ResponseWriter, r *http.Request) {
	cuser = User{}
	ShowView(w, r, "login.html", nil)
}

// 显示添加信息页面
func (app *Application) AddEduShow(w http.ResponseWriter, r *http.Request) {
	data := &struct {
		CurrentUser User
		Msg         string
		Flag        bool
	}{
		CurrentUser: cuser,
		Msg:         "",
		Flag:        false,
	}

	ShowView(w, r, "addEdu.html", data)
}

// 添加信息
func (app *Application) AddEdu(w http.ResponseWriter, r *http.Request) {
	edu := service.Education{
		Name:           r.FormValue("name"),
		Gender:         r.FormValue("gender"),
		Nation:         r.FormValue("nation"),
		EntityID:       r.FormValue("entityID"),
		Place:          r.FormValue("place"),
		BirthDay:       r.FormValue("birthDay"),
		EnrollDate:     r.FormValue("enrollDate"),
		GraduationDate: r.FormValue("graduationDate"),
		SchoolName:     r.FormValue("schoolName"),
		Major:          r.FormValue("major"),
		QuaType:        r.FormValue("quaType"),
		Length:         r.FormValue("length"),
		Mode:           r.FormValue("mode"),
		Level:          r.FormValue("level"),
		Graduation:     r.FormValue("graduation"),
		CertNo:         r.FormValue("certNo"),
		Photo:          r.FormValue("photo"),
	}
	app.Setup.SaveEdu(edu)
	r.Form.Set("certNo", edu.CertNo)
	r.Form.Set("name", edu.Name)
	app.FindCertByNoAndName(w, r)
}

// 显示查询页面
func (app *Application) QueryPage(w http.ResponseWriter, r *http.Request) {
	data := &struct {
		CurrentUser User
		Msg         string
		Flag        bool
	}{
		CurrentUser: cuser,
		Msg:         "",
		Flag:        false,
	}
	ShowView(w, r, "query.html", data)
}

// 根据证书编号和姓名查询信息
func (app *Application) FindCertByNoAndName(w http.ResponseWriter, r *http.Request) {
	certNo := r.FormValue("certNo")
	name := r.FormValue("name")
	result, err := app.Setup.FindEduByCertNoAndName(certNo, name)

	var edu = service.Education{}
	json.Unmarshal(result, &edu)

	fmt.Println("根据证书编号和姓名查询信息成功")
	fmt.Println(edu)

	data := &struct {
		Edu         service.Education
		CurrentUser User
		Msg         string
		Flag        bool
		History     bool
	}{
		Edu:         edu,
		CurrentUser: cuser,
		Msg:         "",
		Flag:        false,
		History:     false,
	}

	if err != nil {
		data.Msg = err.Error()
		data.Flag = true
	}

	ShowView(w, r, "queryResult.html", data)
}

// 显示查询页面
func (app *Application) QueryPage2(w http.ResponseWriter, r *http.Request) {
	data := &struct {
		CurrentUser User
		Msg         string
		Flag        bool
	}{
		CurrentUser: cuser,
		Msg:         "",
		Flag:        false,
	}
	ShowView(w, r, "query2.html", data)
}

// 根据身份证号码查询信息
func (app *Application) FindByID(w http.ResponseWriter, r *http.Request) {
	entityID := r.FormValue("entityID")
	result, err := app.Setup.FindEduInfoByEntityID(entityID)
	var edu = service.Education{}
	json.Unmarshal(result, &edu)

	data := &struct {
		Edu         service.Education
		CurrentUser User
		Msg         string
		Flag        bool
		History     bool
	}{
		Edu:         edu,
		CurrentUser: cuser,
		Msg:         "",
		Flag:        false,
		History:     true,
	}

	if err != nil {
		data.Msg = err.Error()
		data.Flag = true
	}

	ShowView(w, r, "queryResult.html", data)
}

// 显示 修改/添加 信息页面
func (app *Application) ModifyShow(w http.ResponseWriter, r *http.Request) {

	certNo := r.FormValue("certNo")
	name := r.FormValue("name")
	result, err := app.Setup.FindEduByCertNoAndName(certNo, name)

	var edu = service.Education{}
	json.Unmarshal(result, &edu)

	data := &struct {
		Edu         service.Education
		CurrentUser User
		Msg         string
		Flag        bool
	}{
		Edu:         edu,
		CurrentUser: cuser,
		Flag:        true,
		Msg:         "",
	}

	if err != nil {
		data.Msg = err.Error()
		data.Flag = true
	}

	ShowView(w, r, "modify.html", data)
}

// 修改/添加 信息
func (app *Application) Modify(w http.ResponseWriter, r *http.Request) {
	edu := service.Education{
		Name:           r.FormValue("name"),
		Gender:         r.FormValue("gender"),
		Nation:         r.FormValue("nation"),
		EntityID:       r.FormValue("entityID"),
		Place:          r.FormValue("place"),
		BirthDay:       r.FormValue("birthDay"),
		EnrollDate:     r.FormValue("enrollDate"),
		GraduationDate: r.FormValue("graduationDate"),
		SchoolName:     r.FormValue("schoolName"),
		Major:          r.FormValue("major"),
		QuaType:        r.FormValue("quaType"),
		Length:         r.FormValue("length"),
		Mode:           r.FormValue("mode"),
		Level:          r.FormValue("level"),
		Graduation:     r.FormValue("graduation"),
		CertNo:         r.FormValue("certNo"),
		Photo:          r.FormValue("photo"),
	}
	app.Setup.ModifyEdu(edu)
	r.Form.Set("certNo", edu.CertNo)
	r.Form.Set("name", edu.Name)
	app.FindCertByNoAndName(w, r)
}
